import { ref } from 'vue';

/**
 * @description 创建一个状态管理器
 *
 * @param {Number | String | Boolean | Object | Array} initialState 初始状态
 * @returns {[Number | String | Boolean | Object | Array, Function]} [state, updateState]
 */
export const useState = (initialState) => {
  const state = ref(initialState);

  /**
   * 更新状态
   *
   * @param {Number | String | Boolean | Object | Array} nextState
   * @returns void
   */
  const updateState = (nextState) => {
    // 判断是否为函数
    if (typeof nextState === 'function') {
      state.value = nextState(state.value);
    } else {
      state.value = nextState;
    }
  };

  return [state, updateState];
};
